import { api } from '../index'
import axios from 'axios';

export function addMedicationData(record) {
  return api.post('/MedicationData', record)
}

const apiClient = axios.create({
  baseURL: 'http://localhost:8080', // 确保与后端端口一致
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

export function getALLMedicationDataById(id) {
  return apiClient.get(`/MedicationData/${id}`)
    .then(response => {
      console.log('API响应数据:', response.data);
      return response.data;
    })
    .catch(error => {
      console.error('请求失败:', error);
      throw error; // 将错误传递给调用方
    });
}

export function deleteMedicationData(id) { 
  return apiClient.delete(`/MedicationData/${id}`)
  .then(response => {
    console.log('API响应数据:', response.data);
    return response.data;
  })
  .catch(error => {
    console.error('请求失败:', error);
    throw error; // 将错误传递给调用方
  });
}

export function setMedicationData(id,record) {
  return apiClient.put(`/MedicationData/${id}`, record)
  .then(response => {
    console.log('API响应数据:', response.data);
    return response.data;
  })
  .catch(error => {
    console.error('请求失败:', error);
    throw error; // 将错误传递给调用方
  });
}

export function medicationDataFilter(record) {
  return apiClient.post(`/MedicationDataFilter`,record)
  .then(response => {
    console.log('API响应数据:', response.data);
    return response.data;
  })
  .catch(error => {
    console.error('请求失败:', error);
    throw error; // 将错误传递给调用方
  });
}
